#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;

void solve(){
    int n,d;
    string s;
    cin>>n>>d>>s;
    sort(s.begin(), s.end());
    vector<string> ss;

    for(int i=1;i<=n;i++){
        vector<bool> check(n,false);
        fill(check.end()-i,check.end(),true);
        do{
            string num;
            for(int i=0;i<n;i++){
                if(check[i]){
                    num+=s[i];
                }
            }
            sort(num.begin(),num.end());
            do{
                int val=stoi(num);
                if(val%d==0){
                    ss.push_back(num);
                }
            }while(next_permutation(num.begin(), num.end()));  
        }while(next_permutation(check.begin(),check.end()));
    }
    
    if(ss.empty()){
        cout<<-1<<endl;
    }else{
        string res=ss[0];
        for(const string& num:ss){
            if(num.length()<res.length()){
                res=num;
            }
            else if(num.length()==res.length() && num<res){
                res=num;
            }
        }
        cout<<res<<endl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}